﻿using ConsoleApp4.Model;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    /// <summary>
    /// 国家统计局官网 行政区划代码获取
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //使用XPath的模糊查找，基于document或者新建基点查询
            //加载Web 的页面并解析内容
            HtmlWeb web = new HtmlWeb();
            web.OverrideEncoding = Encoding.GetEncoding("gb2312");
            HtmlDocument doc = web.Load("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/index.html");
            HtmlNodeCollection col = doc.DocumentNode.SelectNodes("//tr[@class='provincetr']");

            //统计字段
            var province_cnt = 0;
            var city_cnt = 0;
            var area_cnt = 0;

            //以document为基准查询
            foreach (HtmlNode item in col)
            {
                var tds = item.SelectNodes("td");
                foreach (var td in tds)
                {
                    if (string.IsNullOrEmpty(td.InnerText)) break;
                    var attr = td.FirstChild.Attributes["href"].Value;
                    var href = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/" + attr;
                    var provoince = td.InnerText; //获取省
                    Console.WriteLine("----------------");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{attr.Replace(".html", "")}0000 {provoince}");
                    province_cnt += 1;
                    new DbHelper().Add(new sys_area
                    {
                        code = $"{attr.Replace(".html", "")}0000",
                        parentcode = "100000",
                        name = provoince,
                        shortname = provoince,
                        level = 1
                    }).GetAwaiter().GetResult();

                    //获取市
                    HtmlWeb cityweb = new HtmlWeb();
                    cityweb.OverrideEncoding = Encoding.GetEncoding("gb2312");
                    HtmlDocument citydoc = cityweb.Load(href);
                    HtmlNodeCollection citycol = citydoc.DocumentNode.SelectNodes("//tr[@class='citytr']");
                    Console.WriteLine("++++++++++++++++");
                    foreach (HtmlNode c in citycol)
                    {
                        var citytds = c.SelectNodes("td"); //获取市
                        var cattr = citytds[0].FirstChild.Attributes["href"].Value;
                        var chref = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/" + cattr;
                        var cval = citytds[0].InnerText;
                        var city = citytds[1].InnerText;
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"{cval.Substring(0, 6)} {city}");
                        city_cnt += 1;

                        new DbHelper().Add(new sys_area
                        {
                            code = $"{cval.Substring(0, 6)}",
                            parentcode = $"{attr.Replace(".html", "")}0000",
                            name = city,
                            shortname = city,
                            level = 2
                        }).GetAwaiter().GetResult();
                        
                        //获取区
                        HtmlWeb areaweb = new HtmlWeb();
                        areaweb.OverrideEncoding = Encoding.GetEncoding("gb2312");
                        HtmlDocument areadoc = cityweb.Load(chref);
                        HtmlNodeCollection areacol = areadoc.DocumentNode.SelectNodes("//tr[@class='countytr']");
                        Console.WriteLine("##################");
                        if (areacol == null)
                        {
                            continue;
                        }
                        foreach (HtmlNode d in areacol)
                        {
                            var areatds = d.SelectNodes("td"); //获取区
                            var dval = areatds[0].InnerText;
                            var area = areatds[1].InnerText;
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine($"{dval.Substring(0, 6)} {area}");
                            area_cnt += 1;

                            new DbHelper().Add(new sys_area
                            {
                                code = $"{dval.Substring(0, 6)}",
                                parentcode = $"{cval.Substring(0, 6)}",
                                name = area,
                                shortname = area,
                                level = 3
                            }).GetAwaiter().GetResult();
                        }

                        var random = new Random().Next(0, 40);
                        Task.Delay(random);
                    }
                }
            }

            Console.WriteLine("-----解析结束--------");
            Console.WriteLine($"-----全国共有个{province_cnt}省(不含港澳台)--------");
            Console.WriteLine($"-----全国共有个{city_cnt}市--------");
            Console.WriteLine($"-----全国共有个{area_cnt}区--------");

            Console.ReadLine();

        }
    }
}
